#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <assert.h>
#include <ctype.h>

#define SEP " "
#define NUM 1024
#define SIZE 128

#define DROP_SPACE(s) do { while(isspace(*s)) s++; }while(0)

char command_line[NUM];
char *command_args[SIZE];

char env_buffer[NUM];

#define NONE_REDIR  -1
#define INPUT_REDIR  0
#define OUTPUT_REDIR 1
#define APPEND_REDIR 2

int g_redir_flag = NONE_REDIR;
char *g_redir_filename = NULL;

extern char **environ;

//对应上层的内建命令
int ChangeDir(const char * new_path)
{
    chdir(new_path);

    return 0; // 调用成功
}

void PutEnvInMyShell(char * new_env)
{
    putenv(new_env);
}

void CheckDir(char *commands)
{
    assert(commands);

    char *start = commands;
    char *end = commands + strlen(commands);
    // ls -a -l
    while(start < end)
    {
        if(*start == '>')
        {
            if(*(start + 1) == '>')
            {
                // ls -a -l >> log.txt ---- 追加重定向
                *start = '\0';
                start += 2;
                g_redir_flag = APPEND_REDIR;
                DROP_SPACE(start);
                g_redir_filename = start;
                break;
            }
        
            else
            {
                // ls -a -l > log.txt ---- 输出重定向
                *start = '\0';
                start++;
                DROP_SPACE(start);
                g_redir_flag = OUTPUT_REDIR;
                g_redir_filename = start;
                break;
            }
        }
        else if(*start == '<')
        {
            // 输入重定向
            *start = '\0';
            start++;
            DROP_SPACE(start);
            g_redir_flag = INPUT_REDIR;
            g_redir_filename = start;
            break;
        }
        else
        {
            start++;
        }
    }
}

int main()
{
    // shell 本质上就是一个死循环
    while(1)
    {
        g_redir_flag = NONE_REDIR;
        g_redir_filename = NULL;

        // 1.显示提示符
        printf("[李四@我的主机名 目录]# ");
        fflush(stdout);

        // 2.获取用户输入
        memset(command_line, '\0',  sizeof(command_line) * sizeof(char));
        fgets(command_line, NUM, stdin); // 键盘，标准输入，stdin，获取到的是c风格的字符串，'\0'
        command_line[strlen(command_line) - 1] = '\0'; // 清空\n
       
        // ls -a -l > log.txt or cat < log.txt or ls -a -l >> log.txt or ls -a -l
        // ls -a -l > log.txt -> ls -a -l\0log.txt
        CheckDir(command_line);

        // 3.字符串划分"ls -a -l -i" -> "ls" "-a" "-l" "-i"
        command_args[0] = strtok(command_line, SEP);
        int index = 1;
        if(strcmp(command_args[0]/*程序名*/, "ls") == 0 )
				{ 
            command_args[index++] = (char*)"--color=auto";
				}
        // strtok截取成功，返回字符串起始地址
        // 截取失败，返回NULL
        while(command_args[index++] = strtok(NULL, SEP));

        // 4. 内建命令
        if(strcmp(command_args[0], "cd") == 0 && command_args[1] != NULL)
        {
            ChangeDir(command_args[1]); //让调用方进行路径切换, 父进程
            continue;
        }
        if(strcmp(command_args[0], "export") == 0 && command_args[1] != NULL)
        {
            // 目前，环境变量信息在command_line,会被清空
            // 此处我们需要自己保存一下环境变量内容
            strcpy(env_buffer, command_args[1]);
            PutEnvInMyShell(env_buffer); //export myval=100, BUG?
            continue;
        } 
        
        // 5.创建进程，执行 
        pid_t id = fork();
        if(id == 0)
        {
            int fd = -1;
            switch(g_redir_flag)
            {
                case NONE_REDIR:
                    break;
                case INPUT_REDIR:
                    fd = open(g_redir_filename, O_RDONLY);
                    dup2(fd, 0);
                    break;
                case OUTPUT_REDIR:
                    fd = open(g_redir_filename, O_WRONLY | O_CREAT | O_TRUNC);
                    dup2(fd, 1);
                    break;
                case APPEND_REDIR:
                    fd = open(g_redir_filename, O_WRONLY | O_CREAT | O_APPEND);
                    dup2(fd, 1);
                    break;
                default:
                    printf("Bug?\n");
                    break;
            }
            // child
            // 6.程序替换 不会影响曾经子进程打开的文件
            execvp(command_args[0], command_args);
            exit(1); // 执行到这里，子进程一定替换失败
        }
        int status = 0;
        pid_t ret = waitpid(id, &status, 0);
        if(ret > 0)
        {
            printf("等待子进程成功：sig:%d, code:%d\n", status & 0x7F, (status >> 8) & 0xFF);
        }
    }
}
